﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using UFSSDesktopApp.UFSSservicereference;

namespace UFSSDesktopApp
{
    public partial class FormUpload : Form
    {
        private FileStream fs;
        private UFSSserviceSoapClient ussc;
        private UserCreds ucreds;
        private byte[] filedata;
        private string filename;
        ArrayList sublist = new ArrayList();
        private void FormUpload_Load(object sender, EventArgs e)
        {
            refresh();
            lblStatus.Text = "Select a file and press upload!";
        }
        public void refresh()
        {
            cbSubscription.Items.Clear();
            UFSSSubscription[] subs = ussc.getSubs(ucreds);
            sublist.Clear();
            foreach (UFSSSubscription s in subs)
            {
                sublist.Add(s);
                cbSubscription.Items.Add(s.subName);
            }
            
        }
        public FormUpload(UFSSservicereference.UFSSserviceSoapClient sc, UFSSservicereference.UserCreds uc)
        {
            InitializeComponent();
            ussc = sc;
            ucreds = uc;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            lblSelectedFile.Text = openFileDialog1.SafeFileName;
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            if (openFileDialog1.CheckFileExists)
            {
                if (openFileDialog1.CheckPathExists)
                {
                    filename = openFileDialog1.SafeFileName;
                    string path = openFileDialog1.FileName;
                    fs = new FileStream(path, FileMode.Open);
                    btnUpload.Enabled = true;
                }
                lblStatus.Text = "Click upload to transfer " + filename + ".";
            }

        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                //pbUpload.Visible = true;
                filedata = new byte[fs.Length];
                //pbUpload.Update();
                lblStatus.Text = "Uploading...";
                lblStatus.Refresh();
                int i;
                
                for (i = 0; i < fs.Length; i++)
                {
                    filedata[i] = Byte.Parse(fs.ReadByte().ToString());
                    //pbUpload.Refresh();


                }
                
                string sub = cbSubscription.SelectedItem.ToString();
                if (ussc.UploadFile(ucreds, filedata, filename, sub ))
                {
                    //pbUpload.Update();
                    lblStatus.Text = "Succesful upload!";
                }
                else
                {
                    lblStatus.Text = "Failed to upload!";
                }
                
                //pbUpload.Visible = false;
            }
            catch
            {
                //pbUpload.Visible = false;
                lblStatus.Text = "A problem has occured (check file).";
                btnUpload.Enabled = false;
            }
            try
            {
                fs.Close();
            }
            catch
            { }
            
        }



        private void button1_Click(object sender, EventArgs e)
        {
            Form.ActiveForm.Hide();
            FormMain fm = new FormMain(ussc, ucreds);
            fm.Activate();
            fm.Show();
        }
    }
}
